package com.demo.controller;

import com.demo.entity.MinioProperties;
import com.demo.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author ch3nw3i@gitee
 */
@RestController
@RequestMapping("")
public class OssController {

    @Autowired
    private MinioProperties minioProperties;

    @Autowired
    private MinioUtils minioUtils;


    @PostMapping(value = "/uploadFile")
    public String uploadFile(@RequestBody MultipartFile multipartFile) {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String newFilename = UUID.randomUUID().toString();
        String suffix = multipartFile.getOriginalFilename().split("\\.")[1];
        minioUtils.putObject(minioProperties.getBucketName(), multipartFile, date + "/" + newFilename + "." + suffix);
        String url = "http://" + minioProperties.getEndpoint() + ":" + minioProperties.getPort() + "/" +
                minioProperties.getBucketName() + "/" + date + "/" + newFilename +
                "." + suffix;
        return url;
    }

}
